package com.iot.inclinometer.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.iot.inclinometer.R;
import com.iot.inclinometer.util.BaseUtil;
import com.iot.inclinometer.util.Common;
import com.iot.inclinometer.util.ContextApplication;


/**
 * “设置参数”
 *
 * @author WXR
 */

public class ConfigurationUI extends Activity {
    private String model = null;    //产品型号
    private TextView titleView;        //显示标题(产品型号)

    private EditText A_X_edit = null;
    private EditText A_Y_edit = null;
    private EditText A_Z_edit = null;
    private EditText B_X_edit = null;
    private EditText B_Y_edit = null;
    private EditText B_Z_edit = null;
    private EditText Tclib_edit = null;
    private EditText phiX_edit = null;
    private EditText variance_edit = null;
    private EditText matA11_edit = null;
    private EditText matA12_edit = null;
    private EditText matA13_edit = null;
    private EditText matA21_edit = null;
    private EditText matA22_edit = null;
    private EditText matA23_edit = null;
    private EditText matA31_edit = null;
    private EditText matA32_edit = null;
    private EditText matA33_edit = null;
    private EditText dAX_edit = null;
    private EditText dAY_edit = null;
    private EditText dAZ_edit = null;
    private EditText dBX_edit = null;
    private EditText dBY_edit = null;
    private EditText dBZ_edit = null;
    private EditText model_edit = null;         //产品型号
    private EditText ip_edit = null;           //服务器的IP地址
    private EditText api_edit = null;           //接口地址
    private EditText port_edit = null;        //端口号
    private EditText readnum_edit = null;
    private Button saveBtn;                  //保存按钮
    private Button exitBtn;
    private Button outputMoreBtn;

    private InputMethodManager inputMethodManager;       //控制键盘隐藏
    private boolean isAutoSelect = true;//刚进入页面时下拉选框默认选中

    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE); // 取消标题
        super.onCreate(savedInstanceState);
        setContentView(R.layout.configuration_activity);
        inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        SharedPreferences sharedPreferences = ContextApplication.getAppContext().getSharedPreferences("configuration", 0);
        model = sharedPreferences.getString("Model", "");
        initView();     //初始化控件
        setBtnListener();//设置监听

    }

    /**
     * 初始化控件
     */
    private void initView() {
        titleView = (TextView) findViewById(R.id.tv_top_title);    //初始化标题
		titleView.setText(titleView.getText() + (Common.isEmpty(model) ? "" : "(" + model + ")"));

        A_X_edit = (EditText) findViewById(R.id.A_X_edit);
        A_Y_edit = (EditText) findViewById(R.id.A_Y_edit);
        A_Z_edit = (EditText) findViewById(R.id.A_Z_edit);
        B_X_edit = (EditText) findViewById(R.id.B_X_edit);
        B_Y_edit = (EditText) findViewById(R.id.B_Y_edit);
        B_Z_edit = (EditText) findViewById(R.id.B_Z_edit);
        Tclib_edit = (EditText) findViewById(R.id.Tclib_edit);
        phiX_edit = (EditText) findViewById(R.id.phiX_edit);
        variance_edit = (EditText) findViewById(R.id.variance_edit);
        matA11_edit = (EditText) findViewById(R.id.matA11_edit);
        matA12_edit = (EditText) findViewById(R.id.matA12_edit);
        matA13_edit = (EditText) findViewById(R.id.matA13_edit);
        matA21_edit = (EditText) findViewById(R.id.matA21_edit);
        matA22_edit = (EditText) findViewById(R.id.matA22_edit);
        matA23_edit = (EditText) findViewById(R.id.matA23_edit);
        matA31_edit = (EditText) findViewById(R.id.matA31_edit);
        matA32_edit = (EditText) findViewById(R.id.matA32_edit);
        matA33_edit = (EditText) findViewById(R.id.matA33_edit);
        dAX_edit = (EditText) findViewById(R.id.dAX_edit);
        dAY_edit = (EditText) findViewById(R.id.dAY_edit);
        dAZ_edit = (EditText) findViewById(R.id.dAZ_edit);
        dBX_edit = (EditText) findViewById(R.id.dBX_edit);
        dBY_edit = (EditText) findViewById(R.id.dBY_edit);
        dBZ_edit = (EditText) findViewById(R.id.dBZ_edit);
        model_edit = (EditText) findViewById(R.id.model_edit);
        ip_edit = (EditText) findViewById(R.id.ip_edit);
        api_edit = (EditText) findViewById(R.id.api_edit);
        port_edit = (EditText) findViewById(R.id.port_edit);
        readnum_edit = (EditText) findViewById(R.id.readnum_edit);
        saveBtn = (Button) findViewById(R.id.save_btn); // 保存按钮
        exitBtn = (Button) findViewById(R.id.exit_btn);
        outputMoreBtn = (Button) findViewById(R.id.output_more_btn);
        SharedPreferences sharedPreferences = ContextApplication.getAppContext().getSharedPreferences("configuration", 0);
        if (sharedPreferences.getString("Tclib", "").equals("")) {
            //为空，暂时不作处理
        } else {
            A_X_edit.setText(sharedPreferences.getString("A_X", null));
            A_Y_edit.setText(sharedPreferences.getString("A_Y", null));
            A_Z_edit.setText(sharedPreferences.getString("A_Z", null));
            B_X_edit.setText(sharedPreferences.getString("B_X", null));
            B_Y_edit.setText(sharedPreferences.getString("B_Y", null));
            B_Z_edit.setText(sharedPreferences.getString("B_Z", null));
            Tclib_edit.setText(sharedPreferences.getString("Tclib", null));
            matA11_edit.setText(sharedPreferences.getString("matA11", null));
            matA12_edit.setText(sharedPreferences.getString("matA12", null));
            matA13_edit.setText(sharedPreferences.getString("matA13", null));
            matA21_edit.setText(sharedPreferences.getString("matA21", null));
            matA22_edit.setText(sharedPreferences.getString("matA22", null));
            matA23_edit.setText(sharedPreferences.getString("matA23", null));
            matA31_edit.setText(sharedPreferences.getString("matA31", null));
            matA32_edit.setText(sharedPreferences.getString("matA32", null));
            matA33_edit.setText(sharedPreferences.getString("matA33", null));
            dAX_edit.setText(sharedPreferences.getString("dAX", null));
            dAY_edit.setText(sharedPreferences.getString("dAY", null));
            dAZ_edit.setText(sharedPreferences.getString("dAZ", null));
            dBX_edit.setText(sharedPreferences.getString("dBX", null));
            dBY_edit.setText(sharedPreferences.getString("dBY", null));
            dBZ_edit.setText(sharedPreferences.getString("dBZ", null));
            model_edit.setText(sharedPreferences.getString("Model", null));
            ip_edit.setText(sharedPreferences.getString("Ip", null));
            api_edit.setText(sharedPreferences.getString("api", null));
            port_edit.setText(sharedPreferences.getString("Port", null));
            readnum_edit.setText(sharedPreferences.getInt("ReadNum", 0) + "");
            phiX_edit.setText(sharedPreferences.getString("phiX", null));
            variance_edit.setText(sharedPreferences.getString("variance", null));
        }
        saveBtn.setEnabled(false);//初始化保存按钮不能用，只有修改某个量时，保存按钮才可用
    }

    /**
     * 设置保存按钮的点击监听
     */
    private void setBtnListener() {
        // 此处是保存键，获取各个输入框的值，然后存入数据库
        saveBtn.setOnClickListener(saveBtnClickListener);
        exitBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (saveBtn.isEnabled()) {
                    //未保存时，弹出退出设置确认框
                    sureDialog();
                } else {
                    ConfigurationUI.this.finish();
                }
            }
        });
        outputMoreBtn.setOnClickListener(outputMoreBtnClickListener);
        //设置每个编辑框内容变化事件的监听，若有变化，使保存按钮变为可用
        A_X_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        A_Y_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        A_Z_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        B_X_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        B_Y_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        B_Z_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        Tclib_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        phiX_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        variance_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA11_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA12_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA13_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA21_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA22_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA23_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA31_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA32_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        matA33_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        dAX_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        dAY_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        dAZ_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        dBX_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        dBY_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        dBZ_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });

        model_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        ip_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        api_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        port_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
        readnum_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                saveBtn.setEnabled(true);
            }
        });
    }

    /**
     * 保存按钮监听
     */
    private OnClickListener saveBtnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 取得输入的值
            String A_X = A_X_edit.getText().toString().trim();
            String A_Y = A_Y_edit.getText().toString().trim();
            String A_Z = A_Z_edit.getText().toString().trim();
            String B_X = B_X_edit.getText().toString().trim();
            String B_Y = B_Y_edit.getText().toString().trim();
            String B_Z = B_Z_edit.getText().toString().trim();
            String Tclib = Tclib_edit.getText().toString().trim();
            String phiX = phiX_edit.getText().toString().trim();
            String matA11 = matA11_edit.getText().toString().trim();
            String matA12 = matA12_edit.getText().toString().trim();
            String matA13 = matA13_edit.getText().toString().trim();
            String matA21 = matA21_edit.getText().toString().trim();
            String matA22 = matA22_edit.getText().toString().trim();
            String matA23 = matA23_edit.getText().toString().trim();
            String matA31 = matA31_edit.getText().toString().trim();
            String matA32 = matA32_edit.getText().toString().trim();
            String matA33 = matA33_edit.getText().toString().trim();
            String dAX = dAX_edit.getText().toString().trim();
            String dAY = dAY_edit.getText().toString().trim();
            String dAZ = dAZ_edit.getText().toString().trim();
            String dBX = dBX_edit.getText().toString().trim();
            String dBY = dBY_edit.getText().toString().trim();
            String dBZ = dBZ_edit.getText().toString().trim();
            String model = model_edit.getText().toString().trim();
            String ip = ip_edit.getText().toString().trim();
            String api = api_edit.getText().toString().trim();
            String port = port_edit.getText().toString().trim();

            String readnum = readnum_edit.getText().toString().trim();
            String variance = variance_edit.getText().toString().trim();
            String config[] = new String[29];
            config[0] = A_X;
            config[1] = A_Y;
            config[2] = A_Z;
            config[3] = B_X;
            config[4] = B_Y;
            config[5] = B_Z;
            config[6] = Tclib;
            config[7] = matA11;
            config[8] = matA12;
            config[9] = matA13;
            config[10] = matA21;
            config[11] = matA22;
            config[12] = matA23;
            config[13] = matA31;
            config[14] = matA32;
            config[15] = matA33;
            config[16] = dAX;
            config[17] = dAY;
            config[18] = dAZ;
            config[19] = dBX;
            config[20] = dBY;
            config[21] = dBZ;
            config[22] = model;
            config[23] = ip;
            config[24] = port;
            config[25] = readnum;
            config[26] = phiX;
            config[27] = variance;
            config[28] = api;
            if (BaseUtil.isEmpty(config, ConfigurationUI.this)) {
                SharedPreferences sharedPreferences = getSharedPreferences("configuration", 0);//MODE_WORLD_READABLE
                sharedPreferences.edit()
                        .putString("A_X", A_X)
                        .putString("A_Y", A_Y)
                        .putString("A_Z", A_Z)
                        .putString("B_X", B_X)
                        .putString("B_Y", B_Y)
                        .putString("B_Z", B_Z)
                        .putString("Tclib", Tclib)
                        .putString("matA11", matA11)
                        .putString("matA12", matA12)
                        .putString("matA13", matA13)
                        .putString("matA21", matA21)
                        .putString("matA22", matA22)
                        .putString("matA23", matA23)
                        .putString("matA31", matA31)
                        .putString("matA32", matA32)
                        .putString("matA33", matA33)
                        .putString("dAX", dAX)
                        .putString("dAY", dAY)
                        .putString("dAZ", dAZ)
                        .putString("dBX", dBX)
                        .putString("dBY", dBY)
                        .putString("dBZ", dBZ)
                        .putString("Model", model)
                        .putString("Ip", ip)
                        .putString("Port", port)
                        .putInt("ReadNum", Integer.parseInt(readnum))
                        .putString("phiX", phiX)
                        .putString("variance", variance)
                        .putString("api", api)
                        .commit();
                //弹出吐司，表示保存成功
                Toast.makeText(ConfigurationUI.this, "保存成功！",
                        Toast.LENGTH_LONG).show();
                titleView.setText(titleView.getText() + (Common.isEmpty(model) ? "" : "(" + model + ")"));
                saveBtn.setEnabled(false);
            } else {
                // 弹出吐司，表示保存失败
                Toast.makeText(ConfigurationUI.this, "保存失败！",
                        Toast.LENGTH_LONG).show();
            }
        }
    };

    /**
     * 导出更多按钮监听
     */
    private OnClickListener outputMoreBtnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ConfigurationUI.this, OutputMoreProUI.class);
            startActivity(intent);
        }
    };

    /**
     * 监听返回按键
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (saveBtn.isEnabled()) {
                //未保存时，弹出退出设置确认框
                sureDialog();
            } else {
                ConfigurationUI.this.finish();
            }
            return true;
        }
        return true;
    }

    /**
     * 未保存时，弹出退出设置确认框
     */
    public void sureDialog() {
        AlertDialog.Builder builder = new Builder(this);
        builder.setMessage("设置还未保存，确认退出吗？");
        builder.setTitle("提示");
        builder.setPositiveButton("确定",
                new android.content.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        ConfigurationUI.this.finish();
                    }
                });
        builder.setNegativeButton("取消",
                new android.content.DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.create().show();
    }

    /**
     * 点击空白处时，隐藏键盘
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (ConfigurationUI.this.getCurrentFocus() != null && ConfigurationUI.this.getCurrentFocus().getWindowToken() != null) {
                inputMethodManager.hideSoftInputFromWindow(ConfigurationUI.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}